package com.gydz.gydzbaseframeworkboot.common.utils.page;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 分页工具类
 * 
 * @author caiwf
 */
public class PageUtils extends PageHelper
{

    /**
     * 开始分页
     * @param pageParam
     */
    public static void startPage(PageParam pageParam)
    {
        PageHelper.clearPage();
        String orderBy = pageParam.getsSortCol()+" "+pageParam.getsSortDir();
        if(StringUtils.isBlank(pageParam.getsSortDir())){
            orderBy = "";
        }
        int pageNum = pageParam.getiDisplayStart()/pageParam.getiDisplayLength();
        int length = pageParam.getiDisplayLength();
        PageHelper.startPage(pageNum +1, length, orderBy).setReasonable(true);
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }

    /**
     * 获取分页数据
     * @param list
     * @param pageParam
     * @return
     */
    public static TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        PageInfo pageInfo = new PageInfo<>(list);
        rspData.setaData(pageInfo.getList());
        rspData.setiTotalRecords(pageInfo.getTotal());
        rspData.setiTotalDisplayRecords(pageInfo.getTotal());
        return rspData;
    }
}
